package com.hc.weather.controller;

import com.hc.weather.domain.City;
import com.hc.weather.domain.WeatherResponse;
import com.hc.weather.service.CityService;
import com.hc.weather.service.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller //类中的接口方法既能返回文本给浏览器窗口  又能跳转页面
public class HechiController {

    @Autowired
    CityService cityService;
    @Autowired
    WeatherService weatherService;

    //写一个唯一接口，满足用户使用APP的需要
    @RequestMapping("/weather")  //url:localhost:8080/weather?city=130100
    public ModelAndView weather(Model model, @RequestParam(defaultValue = "110100") String city){
        //准备好页面需要的数据：1.城市列表数据， 2.天气数据
        List<City> cityList = cityService.cityList();
        WeatherResponse weatherResponse = weatherService.queryWeatherForUser(city);

        model.addAttribute("list",cityList);
        model.addAttribute("weatherData",weatherResponse);
        model.addAttribute("cityname",city);

        return new ModelAndView("result");  //result.html   --返回的就是页面的名字
    }



}
